package com.success.application.shop;

import android.content.Intent;
import android.view.View;

import com.google.gson.reflect.TypeToken;
import com.success.application.api.YuanbaoApi;
import com.success.application.base.BaseRecyclerViewAdapter;
import com.success.application.base.BaseRecyclerViewFragment;
import com.success.application.common.param.Params;
import com.success.application.common.result.Result;
import com.success.application.myapplication.R;
import com.success.application.shop.activity.HelpDetailActivity;
import com.success.application.shop.activity.HelpPubActivity;
import com.success.application.shop.activity.LoveDetailActivity;
import com.success.application.shop.adapter.HelpAdapter;
import com.success.application.shop.adapter.LoveAdapter;
import com.success.application.shop.bean.Help;
import com.success.application.user.AccountHelper;
import com.success.application.user.activity.LoginActivity;
import com.success.application.widget.SimplexToast;

import java.lang.reflect.Type;

import butterknife.OnClick;

import static com.success.application.user.UserInfoFragment.REQUEST_CODE_LOGIN;

/**
 * Created by Administrator on 2017/10/27.
 */

public class HelpFragment extends BaseRecyclerViewFragment<Help> {
    public static final String BUNDLE_KEY_HELP = "BUNDLE_KEY_HELP";
    public static final int REQUEST_CODE_HELP = 1; //点击活动详情
    public static final int REQUEST_CODE_CREATE = 2; //发布求助
    private int mCurrentPosition;
    @Override
    protected BaseRecyclerViewAdapter<Help> getRecyclerAdapter() {
        return new HelpAdapter(mContext, BaseRecyclerViewAdapter.ONLY_FOOTER);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.shop_help_fragment_recycler_view;
    }

    @Override
    protected Type getType() {
        return new TypeToken<Result<Help>>() {
        }.getType();
    }
    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void requestData() {
        try {
            Params P = new Params();
            P.put("count",10);
            P.put("pagenum",mPageNo);
            YuanbaoApi.getHelpList(P, mHandler);
        } catch (Exception e) {
            SimplexToast.show(mContext,"读取数据失败！");
            e.printStackTrace();
        }
    }
    @Override
    public void onItemClick(int position, long itemId) {
        //点击活动列表，打开活动详情页面
        this.mCurrentPosition = position;
        Help help = mAdapter.getItem(position);
        Intent intent = new Intent(getActivity(), HelpDetailActivity.class);
        intent.putExtra(BUNDLE_KEY_HELP, help);
        startActivityForResult(intent, REQUEST_CODE_HELP);
    }
    //点击浮动“发布”求助按钮
    @OnClick(R.id.fab_pubconsult)
    void pubConsult() {
        if(AccountHelper.getUserId() != null) {
            Intent intent = new Intent(getActivity(), HelpPubActivity.class);
            startActivityForResult(intent, REQUEST_CODE_CREATE);
        } else {
            Intent intent = new Intent(getActivity(), LoginActivity.class);
            intent.putExtra("loginActivity", "loginActivity");
            startActivityForResult(intent, REQUEST_CODE_LOGIN);
            return;
        }
    }
    //回调
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
//        if (requestCode == REQUEST_CODE_MODIFY && resultCode == 1) {
//            Consult consult = mAdapter.getItem(mCurrentPosition);
//            consult.setPls(data.getStringExtra("pls"));
//            consult.setDzs(data.getStringExtra("dzs"));
//            consult.setIsSupported(data.getStringExtra("isSupported"));
//            mAdapter.updateItem(mCurrentPosition);
//        }
        if (requestCode == REQUEST_CODE_CREATE && resultCode == 1) {
            onTabReselect();
        }
    }
}
